package com.cardgame.model.deck;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;

import java.util.*;

@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
@Setter
public class Deck implements IDeck {
    private static int nextId = 1;
    private Integer id;
    private String name;
    private Map<Integer, Integer> cards; // cardId -> count
    private Integer leaderRoleId;
    private boolean isPlayerControlled; // 标记归属

    public Deck() {
        this.id = nextId++;
        this.cards = new HashMap<>();
        this.isPlayerControlled = true;
    }

    public Deck(String name, Integer leaderRoleId, Map<Integer, Integer> cards) {
        this.id = nextId++;
        this.name = name;
        this.leaderRoleId = leaderRoleId;
        this.cards = cards != null ? cards : new HashMap<>();
        this.isPlayerControlled = true; // 默认玩家卡组
    }

    public void setCards(Map<Integer, Integer> cards) {
        this.cards = cards != null ? cards : new HashMap<>();
    }

    public boolean isPlayerControlled() {
        return isPlayerControlled;
    }

    public void setPlayerControlled(boolean playerControlled) {
        isPlayerControlled = playerControlled;
    }

    public int getRoleCount() {
        return cards.entrySet().stream()
                .filter(e -> e.getKey() < 100)
                .mapToInt(Map.Entry::getValue)
                .sum();
    }

    public int getCardCount() {
        return cards.values().stream().mapToInt(Integer::intValue).sum();
    }
}
